package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.DashBoardDataVo;
import com.cskaoyan.mapper.*;
import com.cskaoyan.utils.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description:主界面用户管理模块的controller：UserController
 * @author: landz
 * @create: 2022-12-29 14:44
 * @version:
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private MarketUserMapper marketUserMapper;
    @Resource
    private MarketGoodsMapper marketGoodsMapper;
    @Resource
    private MarketOrderMapper marketOrderMapper;
    @Resource
    private MarketGoodsProductMapper marketGoodsProductMapper;
    @Resource
    private MarketAddressMapper marketAddressMapper;
    @Resource
    private MarketCollectMapper marketCollectMapper;
    @Resource
    private MarketFootprintMapper marketFootprintMapper;
    @Resource
    private MarketSearchHistoryMapper marketSearchHistoryMapper;
    @Resource
    private MarketFeedbackMapper marketFeedbackMapper;


    /**
     * 根据传入的参数做查询 username的模糊查询以及mobile的精确查询
     *
     * @param info     分页：page\limit\    排序： sort\order
     * @param username
     * @param mobile
     * @return
     * @author: landz
     */
    @Override
    public CommonData<MarketUser> list(BasePageInfo info, String username, String mobile) {

        //配置分页
        PageHelper.startPage(info.getPage(), info.getLimit());

        //根据username和mobile做条件查询 where username like ? and mobile = ?
        //构建Criterion的list
        //先不考虑username和mobile是否为空，在考虑判空。
        MarketUserExample example = new MarketUserExample();
        //排序
        example.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketUserExample.Criteria criteria = example.createCriteria();
        //对username和mobile判空
        if (!StringUtil.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (!StringUtil.isEmpty(mobile)) {
            criteria.andMobileEqualTo(mobile);
        }
        List<MarketUser> list = marketUserMapper.selectByExample(example);

        PageInfo<MarketUser> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    /**
     * 从数据库中查询首页面板需要的goodsTotal/userTotal/productTotal/orderTotal数据返回
     *
     * @return
     * @author: landz
     */
    @Override
    public DashBoardDataVo dashboard() {
        Integer goodsTotal = marketGoodsMapper.selectCount();
        Integer orderTotal = marketOrderMapper.selectCount();
        Integer productTotal = marketGoodsProductMapper.selectCount();
        Integer userTotal = marketUserMapper.selectCount();
        return new DashBoardDataVo(goodsTotal, userTotal, productTotal, orderTotal);
    }

    /**
     * 根据传入的参数做查询 收货人名称name的模糊查询以及用户userId的精确查询
     *
     * @param info
     * @return
     * @author: landz
     */
    @Override
    public CommonData<MarketAddress> addressList(BasePageInfo info) {
        //配置分页
        PageHelper.startPage(info.getPage(), info.getLimit());
        //根据name和id做条件查询 where name like ? and id = ?
        //构建Criterion的list
        //先不考虑name和id是否为空，在考虑判空。
        List<MarketAddress> list = marketAddressMapper.selectAllAddressLimit(info);
        PageInfo<MarketAddress> marketAddressPageInfo = new PageInfo<>(list);
        //排序在sql进行
        return CommonData.data(marketAddressPageInfo);
    }

    /**
     * 根据传入的参数做查询排序  用户id：userId做精确查询 商品id：valueId也是做精确查询
     *
     * @param info
     * @return
     * @author: landz
     */
    @Override
    public CommonData<MarketCollect> collectList(BasePageInfo info) {
        //配置分页
        PageHelper.startPage(info.getPage(), info.getLimit());
        //根据userId和valueId做条件查询 where userId = ? and valueId = ?
        //数据库sql考虑判空
        List<MarketCollect> list = marketCollectMapper.selectAllCollectList(info);
        PageInfo<MarketCollect> marketCollectPageInfo = new PageInfo<>(list);
        //排序在sql进行
        return CommonData.data(marketCollectPageInfo);
    }

    /**
     * 根据传入的参数做查询排序  用户id：userId做精确查询 商品id：goodsId也是做精确查询 ,注意商品id现在是叫goodsId
     *
     * @param info
     * @return
     * @author: landz
     */
    @Override
    public CommonData<MarketFootprint> footPrintList(BasePageInfo info) {
        //先设置分页
        PageHelper.startPage(info.getPage(), info.getLimit());
        //根据userId和goodsId做条件查询 where userId = ? and goodsId = ?
        //数据库sql考虑判空
        List<MarketFootprint> list = marketFootprintMapper.selectAllFootPrintList(info);
        PageInfo<MarketFootprint> marketFootprintPageInfo = new PageInfo<>(list);
        //同样排序在sql语句中进行
        return CommonData.data(marketFootprintPageInfo);
    }

    /**
     * 根据传入的参数做查询排序  用户id：userId做精确查询  关键字：keyword做模糊查询
     *
     * @param info
     * @return
     * @author: landz
     */
    @Override
    public CommonData<MarketSearchHistory> historyList(BasePageInfo info) {
        //分页设置
        PageHelper.startPage(info.getPage(), info.getLimit());
        //根据userId和goodsId做条件查询 where userId = ? and keyword = ?
        //数据库sql考虑判空
        List<MarketSearchHistory> list = marketSearchHistoryMapper.selectAllHistoryList(info);
        PageInfo<MarketSearchHistory> marketSearchHistoryPageInfo = new PageInfo<>(list);
        return CommonData.data(marketSearchHistoryPageInfo);
    }

    /**
     * 根据传入的参数做查询排序  用户名：username做模糊查询  反馈ID：id做精确查询
     *
     * @param info
     * @return
     * @author: landz
     */
    @Override
    public CommonData<MarketFeedback> feedBackList(BasePageInfo info) {
        //分页设置
        PageHelper.startPage(info.getPage(), info.getLimit());
        //根据username和id做条件查询 where username like ? and id = ?
        //数据库sql考虑判空
        List<MarketFeedback> list = marketFeedbackMapper.selectAllFeedBackList(info);
        PageInfo<MarketFeedback> marketFeedbackPageInfo = new PageInfo<>(list);
        return CommonData.data(marketFeedbackPageInfo);
    }

    /**
     * 根据传入的参数用户id 查询记录
     *
     * @param id
     * @return
     * @author: landz
     */
    @Override
    public MarketUser userDetail(Integer id) {
        //分页设置
        //根据传入的用户id做查询
        //数据库再考虑判空 where id = ?
        return marketUserMapper.selectUserDetailById(id);
    }

    /**
     * 用户管理模块中会员管理功能中详情修改数据update接口实现
     * 根据对应的key去修改对应的值value
     * @param marketUser
     * @return void
     * @author landz
     * @create 2022/12/31 15:46
     **/
    @Override
    public void userUpdate(MarketUser marketUser) {
        marketUserMapper.updateByPrimaryKeySelective(marketUser);
    }


}
